// Using jQuery with Other Libraries
try {
jQuery.noConflict();

// Blogger extensions Options
var BloggerExtOptions = {
    core_version: '0.7.1'
};

var BloggerExtUserPrefs = {};

var BloggerExt = {};
jQuery.extend( BloggerExt, {
    extensions: [],
    user_prefs: [],
    hooks: [],

    register: function(flag, opts) {
        BloggerExt.extensions.push(flag);
        if(opts != undefined) jQuery.extend(BloggerExtOptions, opts);
        if (typeof BloggerExt[flag]['user_pref'] != 'undefined') BloggerExt.user_prefs.push(flag);
    },

    registerHook: function (fn) {
        BloggerExt.hooks.push(fn);
    },

    process: function() {
        jQuery(document).ready( function(){

            // Use Cookie User Preference over BloggerExtOptions.
            var cookieUserPrefs = BloggerExt.cookie('BloggerExtUserPrefs');
            if (cookieUserPrefs) {
              try {
                BloggerExtUserPrefs = BloggerExt.parseJSON(cookieUserPrefs);
                jQuery.extend(BloggerExtOptions, BloggerExtUserPrefs);
              }catch(ex) {}
            }

            var flag;
            while( flag = BloggerExt.extensions.pop() ) {
                if(BloggerExtOptions[flag]) { try { BloggerExt[flag](); }catch(e){} }
            }
            // run hooks
            BloggerExt.processHook();
        });
    },

    processHook: function() {
        // Try hook Blogger _WidgetManager_HandleControllerResult
        if(BloggerExt.isListMode() && (typeof BloggerExt._WidgetManager_HandleControllerResult == 'undefined') ) {
            BloggerExt._WidgetManager_HandleControllerResult = _WidgetManager._HandleControllerResult;
            _WidgetManager._HandleControllerResult = function() {
                try {
                    // Call _WidgetManager_HandleControllerResult
                    BloggerExt._WidgetManager_HandleControllerResult.apply(this, arguments);
                    // call register hooks
                    for (var idx in BloggerExt.hooks) {
                        var fn = BloggerExt.hooks[idx];
                        fn.call(this);
                    }
                }catch(e) {
                }
            }
        }
    },

    registerAndProcess: function(flag, opts) {
        BloggerExt.register(flag,opts);
        BloggerExt.process();
    },

    isListMode: function() {
        var isList = false;
        if ( document.location.href.split('/').pop() == "" || document.location.href.split('/').pop() == "index.html") {
            // Suppose the url include index.html
            isList = true;
        }else if(document.location.href.indexOf("/search/label/") >= 0) {
            // Suppose the url search label
            isList = true;
        }else if((document.location.href.indexOf("/search?") >= 0) || (document.location.href.indexOf("archive.html") >= 0)) {
            // Suppose the url include search or archive
            isList = true;
        }
        return isList;
    },

    isItemMode: function() {
        return (!BloggerExt.isListMode());
    }
});

// Add toJSON and parseJSON utilities
(function ($) {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            'array': function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            'boolean': function (x) {
                return String(x);
            },
            'null': function (x) {
                return "null";
            },
            'number': function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            'object': function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        v = x[i];
                        f = s[typeof v];
                        if (f) {
                            v = f(v);
                            if (typeof v == 'string') {
                                if (b) {
                                    a[a.length] = ',';
                                }
                                a.push(s.string(i), ':', v);
                                b = true;
                            }
                        }
                    }
                    a[a.length] = '}';
                    return a.join('');
                }
                return 'null';
            },
            'string': function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            }
        };

    $.toJSON = function(v) {
        var f = isNaN(v) ? s[typeof v] : s['number'];
        if (f) return f(v);
    };

    $.parseJSON = function(v, safe) {
        if (safe === undefined) safe = $.parseJSON.safe;
        if (safe && !/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(v))
            return undefined;
        return eval('('+v+')');
    };

    $.parseJSON.safe = false;

})(BloggerExt);

// Add jQuery Cookie plugins
BloggerExt.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

// Register hide_navbar
BloggerExt.hide_navbar = function() {
    if(BloggerExtOptions.fade_navbar) {
        jQuery('#navbar').fadeTo(0,0).hover( function() { jQuery(this).fadeTo("fast", 1); }, function() { jQuery(this).fadeTo("fast", 0); });
    }else {
        jQuery('#navbar').hide();
    }
};
BloggerExt.hide_navbar.user_pref = function() {
    var prefs = [];
    prefs.push({tab_name: 'core', name: 'hide_navbar', display_name:'Hide Navbar:', data_type:'bool', 'default_value': BloggerExtOptions['hide_navbar'] });
    prefs.push({tab_name: 'core', name: 'fade_navbar', display_name:'Fade Navbar:', data_type:'bool', 'default_value': BloggerExtOptions['fade_navbar'] });
    return prefs;
};
BloggerExt.hide_navbar.update_pref = function(prefs) {
	BloggerExtUserPrefs['hide_navbar'] = (typeof prefs['hide_navbar'] != 'undefined') ? true : false ;
	BloggerExtUserPrefs['fade_navbar'] = (typeof prefs['fade_navbar'] != 'undefined') ? true : false;
};
BloggerExt.register('hide_navbar', { hide_navbar: true, fade_navbar: false} );


// Register hide_sidebar_html_markup
BloggerExt.hide_sidebar_html_markup = function() {
    jQuery('.HTML .title').contains('##HIDEME##').each( function(){ jQuery(this).parent().hide(); } );
};
BloggerExt.hide_sidebar_html_markup.user_pref = function() {
    var prefs = [];
    prefs.push({tab_name: 'core', name: 'hide_sidebar_html_markup', display_name:'Hide Sidebar:', data_type:'bool', 'default_value': BloggerExtOptions['hide_sidebar_html_markup'] });
    return prefs;
};
BloggerExt.hide_sidebar_html_markup.update_pref = function(prefs) {
	BloggerExtUserPrefs['hide_sidebar_html_markup'] = (typeof prefs['hide_sidebar_html_markup'] != 'undefined') ? true : false ;
};
BloggerExt.register('hide_sidebar_html_markup', { hide_sidebar_html_markup: true} );


// User Preference
BloggerExt.user_preference = function() {

  /**
   * Create User Preference.
   */
  function createPrefsElements(prefs) {
        var inputHtmls = {
            'string': function(pref) {
                var chtml = '<label for="'+pref['name']+'">'+ pref['display_name'] +'</label>';
                chtml += '<input type="text" value="'+pref['default_value']+'" name="'+pref['name']+ '" /><br />';
                return chtml;
            },
            'bool': function(pref) {
                var chtml = '<label for="'+pref['name']+'">'+ pref['display_name'] +'</label>';
                var cchecked = (pref['default_value']) ? ' checked="checked"' : '';
                chtml += '<input type="checkbox" value="true" name="'+pref['name']+ '"'+ cchecked +' /><br />';
                return chtml;
            },
            'enum': function(pref) {
                var chtml = '<label for="'+pref['name']+'">'+ pref['display_name'] +'</label>';
                chtml += '<select name="'+pref['name']+'">';
                for(var i=0; i<pref['options'].length; i++) {
                  var opt_value = pref['options'][i]['value'];
                  var opt_display = pref['options'][i]['display'];
                  var cchecked = (pref['default_value'] == opt_value) ? ' selected' : '';
                  chtml += '<option value="'+opt_value+'" '+cchecked+'>'+opt_display+'</option>';
                }
                chtml += '</select><br />';
                return chtml;
            }
        };

        for(var i=0; i<prefs.length; i++) {
            var pref = prefs[i];
            var $prefDiv = $userPrefContainer.find('#pref_'+pref['tab_name']);
            if($prefDiv.size() == 0) {
                $prefDiv = jQuery('<div id="pref_'+pref['tab_name']+'" class="fragment"></div>');
                $userPrefContainer.append($prefDiv);
            }
            var f = inputHtmls[pref['data_type']];
            $prefDiv.append(jQuery(f(pref)));

            var $prefTab = $userPrefTabs.find('#preftab_'+pref['tab_name']);
            if($prefTab.size() == 0) {
                $prefTab = jQuery('<li id="preftab_'+pref['tab_name']+'"></li>').css({'display': 'inline'});
                var $aTab = jQuery('<a href="#pref_'+pref['tab_name']+'">'+pref['tab_name']+'</a>');
                $aTab.click(function(e) {
                  var containId = jQuery(this).parent().attr('id').replace('preftab_', 'pref_');
            $userPrefContainer.find('div.fragment').not('#'+containId).hide();
            $userPrefTabs.find('li').removeClass('tabs-selected');
            jQuery(this).parent().addClass('tabs-selected');
                  jQuery('#'+containId).show();
                  return false;
                });

                $prefTab.append($aTab);
                $userPrefTabs.append($prefTab);
                delete $aTab;
                delete $prefTab;
            }
        }
  };


  /**
   * Update User Preference.
   */
  function updatePrefsElements(ev) {

    function fieldValue(el, successful) {
      var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
      if (typeof successful == 'undefined') successful = true;

      if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
          (t == 'checkbox' || t == 'radio') && !el.checked ||
          (t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
          tag == 'select' && el.selectedIndex == -1))
              return null;

      if (tag == 'select') {
          var index = el.selectedIndex;
          if (index < 0) return null;
          var a = [], ops = el.options;
          var one = (t == 'select-one');
          var max = (one ? index+1 : ops.length);
          for(var i=(one ? index : 0); i < max; i++) {
              var op = ops[i];
              if (op.selected) {
                  // extra pain for IE...
                  var v = jQuery.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value;
                  if (one) return v;
                  a.push(v);
              }
          }
          return a;
      }
      return el.value;
    };

    var user_prefs = {};
    var $inputs = jQuery('#user_pref_container').find('input[@type=checkbox], input[@type=text], select');

    $inputs.each( function(i) {
      var vv = fieldValue(this, true);
      if (vv == null) return;
      user_prefs[this.name] = vv;
    });

    // callback every plugins for update
        for (var i=0; i < BloggerExt.user_prefs.length; i++) {
            var plugin_name = BloggerExt.user_prefs[i];
            if (typeof BloggerExt[plugin_name]['update_pref'] != 'undefined') {
              var plugin_prefs = BloggerExt[plugin_name]['update_pref'].call(document, user_prefs);
            }
        }

    savePrefsElements();
    jQuery(ev.target).attr('disabled', true).val('Saved! Please reload');

  };

  /**
   * Save User Preference.
   */
  function savePrefsElements() {
    var jsonBloggerExtUserPrefs = BloggerExt.toJSON(BloggerExtUserPrefs);
    BloggerExt.cookie('BloggerExtUserPrefs', jsonBloggerExtUserPrefs, {expires: 180});
  };

  /*
   * User Preference Plugin initialize.
   */
    var $userPrefContainer = jQuery('#user_pref_container');
  var $userPrefTabs = null;
  // if user_pref_container div exists
    if($userPrefContainer.size()>0) {
    $userPrefTabs = jQuery('<ul class="tabs-nav anchors"></ul>').css({'list-style-type': 'none', 'list-style-position': 'outside'});
    $userPrefContainer.append($userPrefTabs);
    $userPrefContainer.append('<div class="menuline" style="clear: both;"></div>');

        for (var i=0; i < BloggerExt.user_prefs.length; i++) {
            var plugin_name = BloggerExt.user_prefs[i];
            var plugin_prefs = BloggerExt[plugin_name]['user_pref'].call(document);
            createPrefsElements(plugin_prefs);
        }
    $userPrefContainer.append( jQuery('<input type="button" value="update"></input>').click(updatePrefsElements));
    $userPrefContainer.find('div.fragment').hide().eq(0).show();
    $userPrefTabs.find('li').eq(0).toggleClass('tabs-selected');
    }

};
BloggerExt.register('user_preference', {user_preference: true} );

// Run All Register Process
BloggerExt.process();
}catch (e) { }

